import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

abstract class BaseWidget extends StatefulWidget {
  BaseWidget({Key? key}) : super(key: key);

  @override
  State createState() {
    return getState();
  }

  BaseWidgetState getState();
}

abstract class BaseWidgetState<T extends BaseWidget> extends State<T> {
  bool get showLeading => false;

  @override
  Widget build(BuildContext context) {
    preBuild(context);
    LogUtil.e(" build " + runtimeType.toString());
    return MaterialApp(
      home: Scaffold(appBar: _getAppBar(), body: baseBuild(context)),
    );
  }

  /**
   * 获取页面标题
   */
  String getAppBarTitle();

  bool _showBackLeading = false;

  bool get showBackLeading => _showBackLeading;

  set showBackLeading(bool value) => _showBackLeading = value;

  AppBar _getAppBar() {
    if (_showBackLeading) {
      return AppBar(
        title: Text(getAppBarTitle()),
        leading: InkWell(
          onTap: (){
            Navigator.of(context).pop();
          },
          child: Icon(Icons.arrow_back_ios_sharp),
        ),
      );
    }

    return AppBar(title: Text(getAppBarTitle()));
  }

  Widget baseBuild(BuildContext context);

  @protected
  void preBuild(BuildContext context) {}
}
